<?
if ($_REQUEST['Delete'])
{
	$x = mysql_query("DELETE FROM `Registration-".$_REQUEST['id']."` WHERE `id`='".$_REQUEST['Delete']."'");
	if ($x){
		printf("<script type='text/javascript'>window.location=\"/event/%s/reg\";</script>", $_REQUEST['id']);
	} else {
		printf("<script type='text/javascript'>alert(\"ERROR could not delete id=%s from Registration-%s\");</script>",$_REQUEST['Delete'], $_REQUEST['id']);
	}	
}
if ($_REQUEST['insert']){
	$codes = explode("_", $_REQUEST['insert']);
	for ($z=0;$z<count($codes);$z++){
		$codes[$z] = addslashes($codes[$z]);
	}
	if ($codes[11] == "1"){
		$query = "INSERT INTO `Registration-".$_REQUEST['id']."`";
		$query .= "VALUES('','".$codes[0]."','".$codes[1]."','".$codes[2]."','".$codes[3]."','".$codes[4]."','".$codes[5]."','".$codes[6]."','".$codes[7]."','".$codes[8]."','".$codes[9]."','".$codes[10]."')";
		$x = mysql_query($query);
	} else {
		printf("<script type='text/javascript'>alert(\"not written yet!\");</script>");
	}
	if ($x){
		if (count(explode("/", $_SERVER['REQUEST_URI'])) == 6){
			printf("<script type='text/javascript'>window.location = \"/event/%s/reg/success/Registered+successfully\";</script>", $_REQUEST['id']);
		} else {
			printf("<script type='text/javascript'>window.location = \"/event/%s/success/Thanks+for+registering\";</script>", $_REQUEST['id']);
		}
	} else {
		$newTable = "CREATE TABLE `Registration-$id` (
				  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
				  `names` text,
				  `numRegistered` int(11) DEFAULT '1',
				  `phone` text,
				  `email` text,
				  `notes` text,
				  `paymentType` text,
				  `amountPaid` float DEFAULT NULL,
				  `paidFull` tinyint(4) DEFAULT '0',
				  `scholarship` float DEFAULT '0',
				  `donation` float DEFAULT NULL,
				  `amountOwe` float DEFAULT NULL,
				  PRIMARY KEY (`id`)
				) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
		$test = mysql_query($newTable);
		if ($test){
			printf("<script type='text/javascript'>window.location = window.location</script>");
		} else {
			printf("<script type='text/javascript'>alert(\"Error making table `Registration-$id`\")</script>");
		}
	}
}
if ($_REQUEST['donate'])
{
	$codes = explode("_", $_REQUEST['donate']);
	for ($z=0;$z<count($codes);$z++){
		$codes[$z] = addslashes($codes[$z]);
	}
	$x = mysql_query("INSERT INTO `Registration-".$_REQUEST['id']."` VALUES('','".$codes[0]."','','".$codes[2]."','".$codes[1]."','Donation Only.','".$codes[4]."','','','','".$codes[3]."','')");
	if ($x){
		printf("<script type='text/javascript'>window.location=\"/event/%s/reg\";</script>", $_REQUEST['id']);
	} else {
		printf("<script type='text/javascript'>alert(\"ERROR could not donate %s to Registration-%s\");</script>",$codes[0], $_REQUEST['id']);
	}	
}
function printLogin($id)
{
	printf("<div class='loginBox'>\n");
	printf("\t<form class='loginBoxForm' action='/event/%s/reg' method='post'>\n", $id);
	printf("\t\t<h4>Login</h4><br>\n");
	printf("\t\t<input id='pass' type='password' name='pass' placeholder='Password'>\n");
	printf("\t\t<span id='incorrect' style='color:red'></span>\n");
	printf("\t\t<br><br><input type='submit' value='Login'>\n");
	printf("\t</form>\n");
	printf("</div>\n");
	if ($_REQUEST['pass']){
		if ($_REQUEST['pass'] == "He1sR1sen"){
			printf("<script type='text/javascript'>document.cookie = \"staffCookie=1\";</script>");
			printf("<script type='text/javascript'>document.location=document.location;</script>"); 
		} else {
			printf("<script type='text/javascript'>document.getElementById(\"pass\").style.outline=\"1px solid red\"; document.getElementById(\"incorrect\").innerHTML=\"Incorrect Password\"</script>\n");
		}
	}
}
function printToolBar($prefix)
{
	printf("<div class='topBar'>\n");
	printf("\t<img id='info' src='/images/info.png' onclick='showStats()'></img>\n");
	printf("\t\t<input type='text' style='width:140px;' id='searchBar' placeholder='Search' onkeyup='searchTable(this)'>\n");
	printf("\t\t<span id='clear' onclick='clearToRegTable()' style='margin-left:-15px;cursor:pointer;display:none;'>x</span>\n");
	printf("\t\t<button id='notPaid' onclick='showAllWhoOwe()' style='margin-left:5px;'>Show all who owe</button>\n");
	printf("\t\t<button id='notPaid' onclick='showAllWhoScholar()' style='margin-left:5px;'>Show all who have a scholarship</button>\n");
	printf("\t\t<span id='successMessage' style='margin-left:5px;color:red'>%s</span>\n", $_REQUEST['message']);
	printf("\t\t<span id='addRow' onclick='showNewRegistrationForm(\"%s\")'>+</span>\n", $prefix);
	printStatsBox();
	printDonationForm();
	printf("</div>\n");
}
function printStatsBox()
{
	printf("\t\t<div id='stats' class='stats' style='visibility:hidden;'>\n");
	printf("\t\t\t<div style='float:right;margin-right:4px;cursor:pointer;' onclick='showStats()'>X</div>\n");
	printf("\t\t\tTotal Collected: $<span id='UIGrandTotalPaid'>0.00</span> \n");
	printf("\t\t\t<br><br>Pending Funds: $<span id='UIGrandTotalOwed'>0.00</span> \n");
	printf("\t\t\t<br><br>Extra Donated: <span id='UIDonationPool'>0.00</span><span style='float:right;margin-right:4px;cursor:pointer;' onclick='showDonationForm()'>+</span>\n");
	printf("\t\t\t<br><br>Scholarships Given: $<span id='UIGrandTotalGiven'>0.00</span> \n");
	printf("\t\t\t<br><br>Net Total: $<span id='UIGrandTotal'>0.00</span> \n");
	printf("\t\t\t<br><br>People Registered: <span id='UIGrandTotalRegistered'>0</span>\n");
	printf("\t\t\t<br><br><button onclick='window.open(\"/downloadCSV.php?id=%s\")'>Download full spreadsheet</button>\n", $_REQUEST['id']);
	printf("\t\t</div>\n");
}
function printDonationForm()
{
	printf("\t\t<div class='stats' id='donationForm' style='visibility:hidden;top:260px;height:100px;width:300px;'>\n");
	printf("\t\t\t<div style='float:right;margin-right:4px;cursor:pointer;' onclick='showDonationForm()'>X</div>\n");
	printf("\t\t\t<input type='text' placeholder='Full Name' id='donation-name'> \n");
	printf("\t\t\t<input type='text' placeholder='Email' id='donation-email'><br><br>\n");
	printf("\t\tPhone:<input type='text' maxlength='3' style='width:30px;' id='donation-area' onkeyup='autoTab(this)'>");
	printf("\t\t-<input type='text' maxlength='3' style='width:30px;' id='donation-pre' onkeyup='autoTab(this)'>\n");
	printf("\t\t-<input type='text' maxlength='4' style='width:40px;' id='donation-post'> &nbsp;\n");
	printf("\t\t\t<input type='text' style='width:60px;' placeholder='Amount' id='donation-amount'><br><br>\n");
	printf("\t\t<input type='radio' name='donation-paymentType' value='Cash'>Cash \n");
	printf("\t\t<input type='radio' name='donation-paymentType' value='Check'>Check \n");
	printf("\t\t<input type='radio' name='donation-paymentType' value='Paypal'>Paypal\n");
	printf("\t\t<button style='position:absolute;right:8px;bottom:8px;' onclick='donateOnly(\"%s\")'>Donate</button>", $_REQUEST['id']);
	printf("\t\t</div>\n");
}
function printaddNewRegistrationRow($event, $prefix)
{
	printf("\t<tr id='%s-addNewRegistration' class='dotted_lines row_even updateRows' style='display:none;'>\n", $prefix);
	printf("\t<form id='%s-form'>\n", $prefix);
	printf("\t\t<td style='min-width:262px;'>\n");
	printf("\t\t\t<input type='text' id='%s-firstNames' name='%s-firstNames' placeholder='First Name' onkeyup='countPeople()'>\n", $prefix, $prefix);
	printf("\t\t\t<input type='text' id='%s-lastNames' name='%s-lastNames' placeholder='Last Name'>\n", $prefix, $prefix);
	printf("\t\t\t<div id='%s-people' style='max-width:300px;'></div>\n", $prefix);
	printf("\t\t\t<button onclick='event.preventDefault();addMore(\"%s\");'>+</button>\n", $prefix);
	printf("\t\t</td>\n");
	printf("\t\t<td>\n");
	printf("\t\t\t<input type='text' id='%s-email' style='width:;' onkeyup='validateEmail(this)' placeholder='Email'>\n", $prefix);
	printf("\t\t\t<span style='color:red;' id='%s-email'></span>\n", $prefix);
	printf("\t\t</td>\n");
	printf("\t\t<td style='min-width:142px;'>\n");
	printf("\t\t\t<input type='text' maxlength='3' style='width:30px;' id='%s-area' onkeyup='autoTab(this)'>",$prefix);
	printf("\t\t\t-<input type='text' maxlength='3' style='width:30px;' id='%s-pre' onkeyup='autoTab(this)'>\n", $prefix);
	printf("\t\t\t-<input type='text' maxlength='4' style='width:40px;' id='%s-post' onkeyup='registerPhone(this)'>\n", $prefix);
	printf("\t\t</td>\n");
	printf("\t\t<td style='min-width:55px;'>\n");
	printf("\t\t\t<input type='radio' name='%s-paymentType' onclick='registerPaymentType()' value='Cash'>Cash<br>\n", $prefix);
	printf("\t\t\t<input type='radio' name='%s-paymentType' onclick='registerPaymentType()' value='Check'>Check<br>\n", $prefix);
	printf("\t\t\t<input type='radio' name='%s-paymentType' onclick='registerPaymentType()' value='Paypal'>Paypal\n", $prefix);
	printf("\t\t</td>\n");
	printf("\t\t<td>\n");
	if ($event['costPerPerson']){
		printf("\t\t\tPer Person: $<span id='costPerPerson'>%s</span><br>", $event['costPerPerson']);
	}
	if ($event['costPerCouple']){
		printf("\t\t\tPer Couple: $<span id='costPerCouple'>%s</span><br>", $event['costPerCouple']);
	}
	if ($event['maxCost']){
		printf("\t\t\tMax: $<span id='maxCost'>%s</span><br>", $event['maxCost']);
	}
	printf("\t\t\t<input type='text' id='%s-amountDue' name='amountDue' value='0.00' disabled='true'><br>", $prefix);
	printf("\t\t\t<input type='checkbox' id='%s-scholarship' onclick='applyScholarship()' /> Scholarship\n", $prefix);
	printf("\t\t</td>\n");
	printf("\t\t<td style='display:none;' class='amountOweCell'>N/A</td>\n");
	printf("\t\t<td style='display:none;' class='scholarshipCell'>N/A</td>\n");
	printf("\t\t<td>\n");
	printf("\t\t\t<textarea id='%s-notes' style='width:;height:;max-width:;max-height:;'>Comments...</textarea>\n",$prefix);
	printf("\t\t\t<div style='float:right;float:bottom;'>\n");
	printf("\t\t\t\t<input type='button' value='Cancel' onclick='showNewRegistrationForm(\"%s\")'> \n", $prefix);
	printf("\t\t\t\t<input type='button' onclick='prepSubmit(\"%s\")' value='Add'>\n",$event['id']);
	printf("\t\t\t</div>\n");
	printf("\t\t</td>\n");
	printf("\t</form>\n");
	printf("\t</tr>\n");
}
function printManagementTable($id)
{
	printf("<table id='regResults'>\n");
	printf("\t<tr>\n");
	printf("\t\t<td><h4>Names</h4></td>\n");
	printf("\t\t<td><h4>Email</h4></td>\n");
	printf("\t\t<td><h4>Phone</h4></td>\n");
	printf("\t\t<td><h4>Payment Type</h4></td>\n");
	printf("\t\t<td><h4>Amount Paid</h4></td>\n");
	printf("\t\t<td class='amountOweCell' style='display:none;'><h4>Amount Owe</h4></td>\n");
	printf("\t\t<td class='scholarshipCell' style='display:none;'><h4>Scholarship Given</h4></td>\n");
	printf("\t\t<td><h4>Notes</h4></td>\n");
	printf("\t</tr>\n");
	$event = mysql_fetch_array(mysql_query("SELECT * FROM `events` WHERE `id`='$id'"));
	printAddNewRegistrationRow($event, "0");
	$result = mysql_query("SELECT * FROM `Registration-$id`");
	if (!$result){
		$newTable = "CREATE TABLE `Registration-$id` (
				  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
				  `names` text,
				  `numRegistered` int(11) DEFAULT '1',
				  `phone` text,
				  `email` text,
				  `notes` text,
				  `paymentType` text,
				  `amountPaid` float DEFAULT NULL,
				  `paidFull` tinyint(4) DEFAULT '0',
				  `scholarship` float DEFAULT '0',
				  `donation` float DEFAULT NULL,
				  `amountOwe` float DEFAULT NULL,
				  PRIMARY KEY (`id`)
				) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
		$test = mysql_query($newTable);
		if ($test){
			$result = mysql_query("SELECT * FROM `Registration-$id`");
		} else {
			printf("<script type='text/javascript'>alert(\"Error making table `Registration-$id`\")</script>");
		}
	}
	$i = 1;
	$grandTotalRegistered = 0;
	$grandTotalPaid = 0;
	$grandTotalDonated = 0;
	$grandTotalOwed = 0;
	$grandTotalGiven = 0;
	while ($registration = mysql_fetch_array($result)){
		if ($registration['donation'] != 0 && $registration['numRegistered'] == 0){
			$grandTotalDonated += floatval($registration['donation']);
			continue;
		}
		if ($i%2 == 0){
			printf("\t<tr class='dotted_lines row_even searchable' id='row-%s' onmouseover='showFunctions(\"%s\", this)'>\n", $registration['id'],$registration['id']);
		} else {
			printf("\t<tr class='dotted_lines row_odd searchable' id='row-%s' onmouseover='showFunctions(\"%s\", this)'>\n", $registration['id'],$registration['id']);
		}
		$people = explode(",", $registration['names']);
		printf("\t\t<td><span id='names-%s'>", $registration['id']);
		for ($x=0;$x<count($people);$x++){
			if ($x!=0){
				printf(",");
			}
			printf(" %s", trim($people[$x]));
		}
		printf("\t\t</span><br>\n");
		printf("\t\t\t<span class='hidden' id='%s'>\n", $registration['id']);
		printf("\t\t\t<a style='color:#c44f4f;' onclick='warnBeforeDelete(\"%s\",\"%s\")'>delete</a>\n", $registration['id'], $id);
		printf("\t\t\t<a onclick='showUpdateRow(%s)'>update</a>\n", $registration['id']);
		printf("\t\t\t</span>\n");
		printf("\t\t<br></td>\n");
		printf("\t\t<td><a id='email-%s' href='mailto:%s'>%s</a>\n", $registration['id'], $registration['email'], $registration['email']);
		printf("\t\t<td><span id='phone-%s'>%s</span></td>\n", $registration['id'], $registration['phone']);
		printf("\t\t<td><span id='payment-%s'>%s</span></td>\n", $registration['id'], $registration['paymentType']);
		printf("\t\t<td><span id='amount-%s'>%s</span></td>\n", $registration['id'], number_format($registration['amountPaid'],2));
		printf("\t\t<td style='display:none;' class='amountOweCell'><span >%s</span></td>\n", number_format($registration['amountOwe'],2));
		printf("\t\t<td style='display:none;' class='scholarshipCell'><span>%s</span></td>\n", number_format($registration['scholarship'],2));
		printf("\t\t<td><span id='notes-%s'>%s</span></td>\n", $registration['id'], nl2br($registration['notes']));
		if ($registration['paidFull'] == 0){
			printf("\t\t<td style='display:none;'>!owe!</td>\n");
		}
		if ($registration['scholarship'] != 0){
			printf("\t\t<td style='display:none;'>!scholarship!</td>\n");
		}
		printf("\t</tr>\n");
		printUpdateRow($registration, $people, $i);
		$i++;
		$grandTotalPaid += floatval($registration['amountPaid']);
		$grandTotalRegistered += floatval($registration['numRegistered']); 
		$grandTotalDonated += floatval($registration['donation']);
		$grandTotalOwed += floatval($registration['amountOwe']);
		$grandTotalGiven += floatval($registration['scholarship']);
	}
	printf("</table>\n");
	$grandTotals[0] = $grandTotalPaid;
	$grandTotals[1] = $grandTotalRegistered;
	$grandTotals[2] = $grandTotalDonated;
	$grandTotals[3] = $grandTotalGiven;
	$grandTotals[4] = $grandTotalOwed;
	return $grandTotals;
}
function printUpdateRow($registration, $people, $i)
{
	if ($i%2 == 0){
		printf("\t<tr class='dotted_lines row_even updateRows' id='updateRow-%s' style='display:none;'>\n", $registration['id']);
	} else {
		printf("\t<tr class='dotted_lines row_odd updateRows' id='updateRow-%s' style='display:none;'>\n", $registration['id']);
	}
	printf("\t<form id='%s-form'>", $registration['id']);
	printf("\t\t<td style='min-width:262px;' id='%s-peopleCell'>\n", $registration['id']);
	for ($x=0;$x<count($people);$x++){
		$names = explode(" ", trim($people[$x]));
		printf("\t\t\t<input type='text' id='%s-firstNames' name='%s-firstNames' value='%s' placeholder='First Name' onkeyup=''>\n", $registration['id'], $registration['id'], $names[0]);
		printf("\t\t\t<input type='text' id='%s-lastNames' name='%s-lastNames' value='%s' placeholder='Last Name'>\n", $registration['id'], $registration['id'], $names[1]);
	}
	printf("\t\t<button onclick='event.preventDefault();'>+</button>\n", $registration['id']);
	if ($registration['numRegistered'] == 1){
		printf("\t\t<button onclick='event.preventDefault();unregister(\"%s\", \"%s\")'>-</button>\n", $registration['id'], $_REQUEST['id']);
	} else {
		printf("\t\t<button onclick='event.preventDefault();'>-</button>\n", $registration['id']);
	}
	printf("\t\t</td>\n");
	printf("\t\t<td>\n");
	printf("\t\t\t<input type='text' value='%s' id='%s-email' placeholder='Email' onkeyup='validateEmail(this);'>\n", $registration['email'], $registration['id']);
	printf("\t\t\t<span style='color:red;'></span>\n");
	printf("\t\t</td>\n");
	printf("\t\t<td style='min-width:142px;'>\n");
	$phone = explode(".", $registration['phone']);
	printf("\t\t\t<input type='text' value='%s' maxlength='3' id='%s-area' style='width:30px;'>-\n", $phone[0], $registration['id']);
	printf("\t\t\t<input type='text' value='%s' maxlength='3' id='%s-pre' style='width:30px;'>-\n", $phone[1] , $registration['id']);
	printf("\t\t\t<input type='text' value='%s' maxlength='4' id='%s-post' style='width:40px;'>\n", $phone[2] , $registration['id']);
	printf("\t\t</td>\n");
	printf("\t\t<td style='min-width:55px;'>\n");
	printf("\t\t\t<input type='radio' onclick='registerPaymentType()' name='%s-paymentType' value='Cash'>Cash\n", $registration['id']);
	printf("\t\t\t<br><input type='radio' onclick='registerPaymentType()' name='%s-paymentType' value='Check'>Check\n", $registration['id']);
	printf("\t\t\t<br><input type='radio' onclick='registerPaymentType()' name='%s-paymentType' value='Paypal'>Paypal\n", $registration['id']);
	printf("\t\t\t<input type='hidden' id='%s-paymentType' value='%s'>\n", $registration['id'], $registration['paymentType']);
	printf("\t\t</td>\n");
	printf("\t\t<td>\n");
	printf("\t\t\t<input type='text' id ='%s-UIAmountPaid' name='amountPaid' value='%s'>\n", $registration['id'], number_format($registration['amountPaid'],2));
	printf("\t\t<br><input type='checkbox' id='%s-scholarshipBox' onclick='applyScholarship()' />Scholarship\n", $registration['id']);
	printf("\t\t</td>\n");
	printf("\t\t<td style='display:none;' class='amountOweCell'><span id='%s-amountOwe'>%s</span></td>\n", $registration['id'], number_format($registration['amountOwe'],2));
	printf("\t\t<td style='display:none;' class='scholarshipCell'><span id='%s-scholarship'>%s</span></td>\n", $registration['id'], number_format($registration['scholarship'],2));
	printf("\t\t<td style='display:none;'><span id='%s-paidInFull'>%s</span>\n", $registration['id'], number_format($registration['paidInFull'],2));
	printf("\t\t<span id='%s-donation'>%s</span>\n", $registration['id'], number_format($registration['donation'],2));
	printf("\t\t<span id='%s-donation'>%s</span>\n", $registration['id'], number_format($registration['donation'],2));
	printf("\t\t<span id='%s-amountPaid'>%s</span></td>\n", $registration['id'], number_format($registration['amountPaid'],2));
	printf("\t\t<td>\n");
	printf("\t\t\t<textarea id='%s-notes' style=''>%s</textarea>\n",$registration['id'], $registration['notes']);
	printf("\t\t<div style='float:right;'>\n");
	printf("\t\t\t<button onclick='event.preventDefault();showUpdateRow(\"%s\");'>Cancel</button>\n", $registration['id']);
	printf("\t\t\t<button onclick='event.preventDefault();'>Update Info</button>\n");
	printf("\t\t</div>\n");
	printf("\t\t</td>\n");
	printf("\t</form>\n");
	printf("\t</tr>\n");
}
?>